import React, { Component } from 'react';
import { connect } from 'react-redux';
import HeaderDefault from '../../components/header/HeaderDefault'
import SearchBar from '../../components/searchBar/SearchBar'

import {
    StyleSheet,
    View,
    Text,
    FlatList
} from "react-native";
import { Empty } from '../../components/Empty/Empty';
import { getAdviseRecList } from '../../netServer/advicePerform';
import { dateTransform } from '../../utils/utils_date';
import { isNotEmptyArray, isObject } from '../../utils/structureJudgment';



class PerformHistorys extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isFresh: false,
            taskList: []
        };

        this.list_data = [];
        this.searchStr = '';
    }

    static navigationOptions = ({ navigation }) => ({
        gesturesEnabled: false,
        header: (
            <HeaderDefault
                title="执行记录"
                backBtnOnPress={() => {
                    navigation.goBack();
                }}

            />
        )
    });

    componentDidMount() {


        this._loadTodayLineTask()

    }

    _refreshData = () => {
        this.setState({
            isFresh: true
        })
        this._loadTodayLineTask()
    }

    _loadTodayLineTask = () => {

        let params = new FormData();

        let { user_id } = this.props.user_info;

        let nowDate = new Date();

        let start_dat = dateTransform(nowDate, 'yyyy-MM-dd') + ' 00:00:00';
        let end_dat = dateTransform(nowDate, 'yyyy-MM-dd') + ' 23:59:59';

        params.append("userId", user_id);
        params.append("startTime", start_dat);
        params.append("endTime", end_dat);
        params.append("concatQuery", this.searchStr);
        params.append("advType", 'med');

        params.append("pageSize", 500);
        params.append("currentPage", 1);

        getAdviseRecList(params, resp => {


            let { code, data = {}, msg } = resp
            if (code == 0) {
                this.setState({
                    taskList: data.data,
                    isFresh: false
                })
            } else {
                this.setState({
                    taskList: [],
                    isFresh: false
                })
            }

        })

    }

    _submitSearch = (searchText) => {
        this.searchStr = searchText;
        this._refreshData();
    }

    _ListEmptyComponent() {

        return <Empty titleStr="暂时没有相关的数据" />
    }

    _separator() {
        return <View style={{ height: 1, backgroundColor: '#efefef' }} />
    }

    renderTimeString(obj) {

        obj = obj.resFreqTime ? obj.resFreqTime : {}


        if (isObject(obj)) {

            var cycleType = obj.cycleType;
            var timeArr;
            if (cycleType == 'week') {
                timeArr = obj.weekList;
            }
            else if (cycleType == 'hour') {
                timeArr = obj.hourList;
            } else {
                timeArr = obj.tempList;
            }



            if (isNotEmptyArray(timeArr)) {
                var resultStr = null;
                timeArr.forEach(element => {

                    var firstS = resultStr ? resultStr + ',' : '';
                    if (cycleType == 'week') {

                        if (element == 1) element = '星期一';
                        else if (element == 2) element = '星期二';
                        else if (element == 3) element = '星期三';
                        else if (element == 4) element = '星期四';
                        else if (element == 5) element = '星期五';
                        else if (element == 6) element = '星期六';
                        else if (element == 0) element = '星期日';

                    }
                    resultStr = firstS + element;
                });
                return resultStr;

            } else {
                return null;
            }


        } else {
            return null;
        }

    }

    _keyExtractor = (item, index) => index + 'performHistorys'

    _renderItem = ({ item }) => {

        const { medList = [], name = '', bed_no = '', inhosp_no = '', exe_time = '', real_name = '', freqTime = {} } = item;

        let userName = '';
        let freqCode = '';

        let dt = exe_time ? new Date(exe_time.replace(/-/g, "/")) : null;




        return (

            <View style={{
                width: screenWidth,
                borderBottomColor: '#efefef',
                borderBottomWidth: 1,
                backgroundColor: 'white',

            }}>

                <View style={{ paddingLeft: 10, paddingRight: 10, paddingTop: 10, justifyContent: 'space-between', flexDirection: 'row' }}>
                    <Text>{name}</Text>
                    <Text>{bed_no}</Text>
                    <Text>{inhosp_no}</Text>
                </View>

                <View style={{ paddingLeft: 15, paddingRight: 10, paddingTop: 10, paddingBottom: 10, borderBottomColor: '#eee', borderBottomWidth: 1, justifyContent: 'space-between', flexDirection: 'row' }}>

                    <Text style={{ color: 'red' }}>{'执行人：' + real_name}</Text>
                    <Text>{''}</Text>
                    <Text style={{ color: 'red' }}>{'执行时间：' + dateTransform(new Date(dt), 'hh:mm:ss')}</Text>
                </View>

                <View
                    style={{
                        width: screenWidth,
                        borderBottomColor: '#efefef',
                        borderBottomWidth: 1,
                        backgroundColor: 'white',
                        flexDirection: 'row'
                    }}
                >


                    <View
                        style={{
                            paddingLeft: 10,
                            paddingRight: 10,
                            width: 0.5 * screenWidth,
                            flexDirection: 'row',
                            alignItems: 'center'
                        }}
                    >


                        <View style={{ flex: 1 }}>
                            {medList.map((value, index) => {
                                userName = value.name
                                freqCode = value.code

                                let status_name = value.status_name

                                return (
                                    <View
                                        style={{
                                            borderBottomColor: '#f1f1f1',
                                            borderBottomWidth: 1,
                                            paddingLeft: 10
                                        }}
                                        key={index}
                                    >
                                        <Text style={{ paddingBottom: 5, paddingTop: 5 }}>
                                            {value.com_name}&nbsp;&nbsp;&nbsp;&nbsp;

                                    </Text>
                                        <Text style={{ paddingBottom: 5, paddingTop: 5 }}>
                                            {value.standard}&nbsp;&nbsp;&nbsp;&nbsp;
                                            {value.measure}{value.take_unit}&nbsp;&nbsp;&nbsp;&nbsp;
                                        </Text>
                                    </View>
                                )
                            })}
                        </View>

                    </View>
                    <View
                        style={{
                            flex: 1,

                            flexDirection: 'row',
                            alignItems: 'center'
                        }}
                    >
                        <Text>{userName}&nbsp;&nbsp;&nbsp;&nbsp;</Text>

                        <Text>
                            {freqCode}&nbsp;&nbsp;&nbsp;&nbsp;
                            {this.renderTimeString(freqTime)}

                        </Text>
                    </View>
                </View>
            </View>

        )
    }

    render() {
        return (
            <View style={{ display: 'flex', flex: 1 }}>
                <SearchBar submitSearch={this._submitSearch} placeholderStr='输入姓名、床号、疗养号搜索' />

                <FlatList
                    refreshing={this.state.isFresh}
                    onRefresh={this._refreshData}
                    style={{ flex: 1 }}
                    ListEmptyComponent={this._ListEmptyComponent}
                    ItemSeparatorComponent={this._separator}
                    data={this.state.taskList}
                    keyExtractor={this._keyExtractor}
                    renderItem={this._renderItem}
                />

            </View>
        );
    }
}

function select(store) {
    return {
        user_info: store.userInfo.user_info
    };
}
export default connect(select)(PerformHistorys);